package com.steven.ourcontest.report.view

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.report.adapter.ReportAdapter
import com.steven.ourcontest.report.viewmodel.HandleReportViewModel
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.activity_handle_report.*
import kotlinx.android.synthetic.main.dialog_confirm_cancel.*

class HandleReportActivity : BaseActivity() {
    override fun getStatusBarColor(): Int {
        return R.color.white
    }

    private val adapter by lazy {
        ReportAdapter(this) {
            viewModel.updateReport(it)
        }
    }
    private val viewModel: HandleReportViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getViewId(): Int? {
        return R.layout.activity_handle_report
    }

    override fun initView() {
        recycler_container?.adapter = adapter
        //refresh
        smart_refresh?.setOnRefreshListener {
            adapter.listData.clear()
            viewModel.refresh()
        }
        smart_refresh?.setOnLoadMoreListener {
            viewModel.loadMore()
        }
        viewModel.reportRes.observe(this, Observer {
            if (it == null) {
                toast("信息获取失败！")
                if (smart_refresh?.isRefreshing == true) {
                    smart_refresh?.finishRefresh()
                }
                if (smart_refresh?.isLoading == true) {
                    smart_refresh?.finishLoadMore()
                }
            } else {
                adapter.listData.addAll(it.data)
                adapter.notifyDataSetChanged()
                if (smart_refresh?.isRefreshing == true) {
                    smart_refresh?.finishRefresh()
                }
                if (smart_refresh?.isLoading == true) {
                    smart_refresh?.finishLoadMore()
                }
            }
        })
        viewModel.updateRes.observe(this, Observer {
            adapter.listData.clear()
            viewModel.refresh()
        })
        viewModel.loadMore()
    }

    override fun initSmartRefresh(): SmartRefreshLayout? {
        return smart_refresh
    }
}